#include "client.h"

char jwt[JWT_LEN];
toml_table_t* conf;

int main(int argc, char *argv[]) {
  //ARGS_CHECK(argc, 3);
  conf_init(&conf);
  client_configure c;
  conf_client(&c);
  int sockfd = tcp_init(c.host, c.port);

  int epfd = epoll_create(1);
  epoll_add(epfd, sockfd);
  epoll_add(epfd, STDIN_FILENO);

  struct epoll_event ready[1024];
  char buf[4096];

    while (1) {
        int ready_num = epoll_wait(epfd, ready, 1024, -1);
        for (int i = 0; i < ready_num; ++i) {
            int fd = ready[i].data.fd;
            if (fd == STDIN_FILENO) {
                memset(buf, 0, sizeof(buf));
                read(STDIN_FILENO, buf, sizeof(buf));
                deal_command(sockfd, c.host, c.port2, buf);
                //deal_command(sockfd, buf);
                // send_tlv(sockfd, buf, read_ret);
            }
            
            //==@wry:No.1 只有加上这一段，超时踢人才能实现客户端退出|START==
             else if(fd == sockfd){ 
                 int read_ret = recv(sockfd,buf,sizeof(buf),0); 
                 if(read_ret == 0){ 
                     printf("be kicked out\n"); 
                     pthread_exit(0); 
                 } 
             } 
            //==@wry:No.1 只有加上这一段，超时踢人才能实现客户端退出|END==

        }
    }

    return 0;
}
